/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import static com.cea.papyrus.diagram.sequence.sedi.model.util.ModelUtil.createUniqueName;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.Lifeline;
import org.eclipse.uml2.uml.UMLFactory;

/**
 * Command to create instances of Lifelines. <!--If the owner of the Interaction
 * is a Class or an Interface, a corresponding Property is created and the
 * Lifeline is configured to represent this Property.-->
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>Interaction
 * </ul>
 * 
 * <p>
 * The following attributes can be read to access the result of the command:
 * </p>
 * <ul>
 * <li>Lifeline
 * </ul>
 * 
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class CreateLifelineCommand extends Command {

	// 15.11.2006 FD
	// Creation of a Property for Lifeline.Represents was
	// removed since the new direct-edit rules for Lifelines
	// do not allow renaming.

	private Interaction interaction;
	private Lifeline lifeline;

	// private CreatePropertyCommand createPropertyCmd;

	public void setInteraction(final Interaction interaction) {
		this.interaction = interaction;
	}

	public Lifeline getLifeline() {
		return lifeline;
	}

	/*
	 * @Override public boolean canExecute() { return interaction != null; }
	 */

	@SuppressWarnings("unchecked")
	@Override
	public void execute() {
		lifeline = UMLFactory.eINSTANCE.createLifeline();
		lifeline.setName(createUniqueName(interaction.getLifelines(),
				"Lifeline_"));
		interaction.getLifelines().add(lifeline);

		/*
		 * createPropertyCmd = new CreatePropertyCommand();
		 * createPropertyCmd.setOwner(interaction.getContext());
		 * createPropertyCmd.execute();
		 * lifeline.setRepresents(createPropertyCmd.getProperty());
		 */
	}

	@Override
	public void undo() {
		// createPropertyCmd.undo();
		interaction.getLifelines().remove(lifeline);
	}

	@Override
	public void redo() {
		interaction.getLifelines().add(lifeline);
	}

	@Override
	public void dispose() {
		lifeline = null;
		interaction = null;
		// createPropertyCmd = null;
	}
}
